package com.edu.exam.entity.permission;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 权限实体类
 * 对应数据库表：permission
 * 管理系统权限信息，包括权限代码、名称、资源类型等
 * 支持权限的创建、查询、更新和删除操作
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Table(name = "permission")
@EntityListeners(AuditingEntityListener.class)
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class Permission {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotBlank(message = "权限代码不能为空")
    @Size(max = 100, message = "权限代码长度不能超过100个字符")
    @Column(name = "permission_code", unique = true, nullable = false, length = 100)
    private String permissionCode;

    @NotBlank(message = "权限名称不能为空")
    @Size(max = 200, message = "权限名称长度不能超过200个字符")
    @Column(name = "permission_name", nullable = false, length = 200)
    private String permissionName;

    @Size(max = 50, message = "资源类型长度不能超过50个字符")
    @Column(name = "resource_type", length = 50)
    private String resourceType;

    @Column(name = "description", columnDefinition = "TEXT")
    private String description;

    @Column(name = "is_built_in", nullable = false)
    private Boolean isBuiltIn = false;

    @Column(name = "sort_order")
    private Integer sortOrder = 0;

    @Column(name = "deleted", nullable = false)
    private Boolean deleted = false;

    @CreatedDate
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    @LastModifiedDate
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    @Column(name = "created_by", length = 50)
    private String createdBy;

    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    public Permission(String permissionCode, String permissionName, String resourceType) {
        this.permissionCode = permissionCode;
        this.permissionName = permissionName;
        this.resourceType = resourceType;
    }
}